<?php
/**
 * Created by PhpStorm.
 * User: liball
 * Date: 2015-07-07
 * Time: 14:41
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Config;
use App\Models\Game;
use Illuminate\Http\Request;

class ConfigController extends Controller
{

    public function index()
    {
        $configs = Config::orderBy('updated_at', 'DESC')->paginate(20);
        return view('admin.configs.index', compact('configs'));
    }


    public function edit($id)
    {
        $config = Config::findOrNew($id);
        return view('admin.configs.edit', compact('config'));
    }

    public function update($id, Request $request)
    {
        $config = Config::find($id);
        $data = $request->except('_token');
        if(!isset($data['check'])){
            $data['check'] = false;
        }
        if(!isset($data['rate'])){
            $data['rate'] = false;
        }
        $config->update($data);
        return redirect()->to('admin/configs');
    }

    public function create()
    {
        return view('admin.configs.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, Config::$rules);
        $data = $request->except('_token');
        if(!$request->has('check')){
            $data['check'] = false;
        }
        if(!$request->has('rate')){
            $data['rate'] = false;
        }
        Config::create($data);
        return redirect()->to('admin/configs');
    }

    public function process(Request $request)
    {
        $action = $request->get('action');
        $ids = $request->get('ids');
        if ($action == 'delete') {
            foreach ($ids as $id) {
                Config::destroy($id);
            }
        }
        return redirect()->back();

    }
}